/*
	The MIT License

	Copyright (c) 2008 IFMO/GameDev Studio

	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.
*/
// win_console.h - 

#pragma once


const int SIZE_X = 80;
const int SIZE_Y = 30;


class	EEditCmdLine {
	public:
					EEditCmdLine	( void );
					~EEditCmdLine	( void );
		void		InsertChar		( char ch );
		void		Left			( void );
		void		Right			( void );
		void		Backspace		( void );
		void		Delete			( void );
		void		Clear			( void );
		void		SetString		( const char *str );
	public:
		int			cursor;
		string		cmdline;
	};


class	EWinConsole	: public IConsole
	{
	public:
							EWinConsole		( void );
		virtual				~EWinConsole	( void );
		
		virtual	void		WriteLine		( const char *buffer );	
		virtual void		SetCmdLine		( const char *buffer );
		virtual bool		GetCmdLine		( char *buffer, uint size );

		/*virtual const char *GetLine			( uint line );			
		virtual uint		GetLineNum		( void );
		*/

	private:
		HANDLE			con_mutex;
		HANDLE			hinput;
		HANDLE			houtput;
		vector<string>	lines;
		queue<string>	cmd_queue;
		uint			scroll;
		
		EEditCmdLine	cmdline;
		
		CHAR_INFO		buffer[SIZE_X * SIZE_Y];
		uint			current_color;
		
		void		CreateConsoleWindow		( void );
		void		DestroyConsoleWindow	( void );

		void		UpdateConsole			( void );
		void		DrawLines				( void );
		void		DrawCmdLine				( void );
		void		DrawInfoLine			( void );		
		void		DrawChar				( int x, int y, char ch );
		void		DrawString				( int x, int y, const char *str );
		void		DrawStringClr			( int x, int y, const char *str );
		void		SetColor				( uint clr ) { current_color = clr; }
		
		void		CreateLogFile			( const char *name );
		void		CloseLogFile			( void );

		HANDLE	hcon_thread;
		static unsigned __stdcall ConsoleThread	( EWinConsole *self );
	};	